package main

import (
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
	"image"
	"log"
)

type Player struct {
	image  *ebiten.Image
	width  int
	height int
	x      float64
	y      float64
	speed  float64
	// 角色是否移动
	playerMoving bool
	// 角色前进方向(用于分割精灵图)
	playerDir int
	// 角色移动方向
	playerUp, playerDown, playerRight, playerLeft bool
	// 角色帧计数
	playerFrame int
	// 精灵图的X位置
	playerScrX int
	// 精灵图的Y位置
	playerSrcY int
	// 是否在种花
	isPlanting bool
	// 判断地块状态
	mapIsPlanting map[int]*TileState
	// 当前所在地块索引
	currentTileIndex int
}

type TileState struct {
	// 是否已种植
	isPlanted bool
	// 花是否已经开放
	isMature bool
	// 从种植到开花计时器
	timer int
}

func NewPlayer(sceneMap *SceneMap) *Player {
	// 总的一张精灵图
	AllImage, _, err := ebitenutil.NewImageFromFile("../Image/Characters/BasicCharakterSpritesheet.png")
	if err != nil {
		log.Fatal(err)
	}
	// xo、y0为起始位置    x1和y1为场地
	norImage := AllImage.SubImage(image.Rect(144, 0, 192, 48)).(*ebiten.Image)
	weight := 48
	height := 48

	// 处理mapIsPlanting
	mapIsPlanting := make(map[int]*TileState)
	for k, v := range sceneMap.sliceMapTyp {
		if v == "t" {
			mapIsPlanting[k] = &TileState{
				isPlanted: false,
			}
		}
	}

	player := &Player{
		image:         norImage,
		width:         weight,
		height:        height,
		x:             100,
		y:             50,
		speed:         1.3,
		playerMoving:  false,
		playerDir:     0,
		playerUp:      false,
		playerDown:    false,
		playerLeft:    false,
		playerRight:   false,
		playerScrX:    0,
		playerSrcY:    0,
		isPlanting:    false,
		mapIsPlanting: mapIsPlanting,
	}
	return player
}

func (player *Player) Draw(screen *ebiten.Image, cfg *Config) {
	/*
		帧数取值有0 1 2 3
		playerDir取值有0 1 2 3
		根据值去切精灵图
		第一行：
		第一张图片取值 x0 = 0, y0 = 0, x1 = 48, y1 = 48
		第二张图片取值 x0 = 48, y0 = 0, x1 = 96, y1 = 48
		第三张图片取值 x0 = 96, y0 = 0, x1 = 144, y1 = 48
		第四张图片取值 x0 = 144, y0 = 0, x1 = 192, y1 = 48
	*/
	AllImage, _, err := ebitenutil.NewImageFromFile("../Image/Characters/BasicCharakterSpritesheet.png")
	AllImagePlant, _, err := ebitenutil.NewImageFromFile("../Image/Characters/BasicCharakterActions.png")
	if err != nil {
		log.Fatal(err)
	}
	var nowImage *ebiten.Image
	if player.isPlanting {
		// 0: 0, 48*8  48, 48*8
		player.playerScrX = player.playerFrame * 48
		player.playerSrcY = 48 * 8
		nowImage = AllImagePlant.SubImage(image.Rect(player.playerScrX, player.playerSrcY, player.playerScrX+48, player.playerSrcY+48)).(*ebiten.Image)
	} else {
		player.playerScrX = player.playerFrame * 48
		player.playerSrcY = player.playerDir * 48
		nowImage = AllImage.SubImage(image.Rect(player.playerScrX, player.playerSrcY, player.playerScrX+48, player.playerSrcY+48)).(*ebiten.Image)
	}
	player.image = nowImage
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(player.x, player.y)
	screen.DrawImage(player.image, op)
}

func (player *Player) CanMoveTo(newX, newY float64, sceneMap *SceneMap) bool {
	// 根据玩家将要移动到的位置映射出地图坐标
	mapX := int(newX+16) / 16
	mapY := int(newY+16) / 16

	// 检查玩家是否在地图边界内
	if mapX < 0 || mapX >= sceneMap.mapW || mapY < 0 || mapY >= sceneMap.mapH {
		return false
	}

	// 获取即将移动到的位置的地图类型
	mapType := sceneMap.sliceMapTyp[mapY*sceneMap.mapW+mapX]

	// 检查地图类型是否为可通过的类型
	if mapType == "g" || mapType == "t" {
		return true
	}

	return false
}

func (player *Player) canPlanting(sceneMap *SceneMap) bool {
	mapX := int(player.x+16) / 16
	mapY := int(player.y+16) / 16
	// 更新对应地块索引
	player.currentTileIndex = mapY*sceneMap.mapW + mapX

	mapType := sceneMap.sliceMapTyp[player.currentTileIndex]
	if mapType == "t" && !player.mapIsPlanting[player.currentTileIndex].isPlanted {
		player.mapIsPlanting[player.currentTileIndex].isPlanted = true
		player.mapIsPlanting[player.currentTileIndex].timer = 0
		return true
	}
	return false
}
